package com.googlecode.dgwt.client.dijit.event;

import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HasHandlers;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public class ChangingEvent<I> extends GwtEvent<ChangingHandler<I>> {

    /**
     * Handler type.
     */
    private static Type<ChangingHandler<?>> TYPE;

    /**
     * Fires a changing event on all registered handlers in the handler
     * manager. If no such handlers exist, this method will do nothing.
     * 
     * @param <I> the old value type
     * @param source the source of the handlers
     */
    public static <I> void fire(HasHandlers source) {
        if (TYPE != null) {
            source.fireEvent(new ChangingEvent<I>());
        }
    }

    /**
     * Gets the type associated with this event.
     * 
     * @return returns the handler type
     */
    public static Type<ChangingHandler<?>> getType() {
        if (TYPE == null) {
            TYPE = new Type<ChangingHandler<?>>();
        }
        return TYPE;
    }

    @Override
    protected void dispatch(ChangingHandler<I> handler) {
        handler.onChanging(this);
    }

    @Override
    @SuppressWarnings("unchecked")
    public Type<ChangingHandler<I>> getAssociatedType() {
        return (Type)TYPE;
    }

    /**
     * Creates a value change event.
     * 
     * @param value the value
     */
    protected ChangingEvent() {
    }

}
